package com.piz.dao.common;

import java.util.List;
import com.piz.model.City;
import com.piz.model.State;


@SuppressWarnings("unchecked")
public class CityDao extends PizHiberImpl{
	
	/**
	 * @param city
	 * @return
	 */
	public Long saveCity(City city)
	{
		try {
			saveOrUpdate(city);
			return city.getId();
		} catch (Exception e) {
			throw new UnexpectedDBException("Error in saving city details.", e);
		}
	}
	
	/**
	 * @param state
	 * @return
	 */
	public Long saveState(State state)
	{
		try {
			saveOrUpdate(state);
			return state.getId();
		} catch (Exception e) {
			throw new UnexpectedDBException("Error in saving state details.", e);
		}
	}
	
	public List<City> getCityInState(Long stateId)
	{
		try {
			List<State> state =  (List<State>) executeHQL("from State where id='"+stateId+"'");
			List<City> citiesinstate = null;
			if(state.size()>0)
			{
				citiesinstate = (List<City>) executeHQL("from City where stateId ="+state.get(0).getId());
			}
			return citiesinstate;
		} catch (Exception e) {
			throw new UnexpectedDBException("No city is available in specified state", e);
		}
	}
	
	public List<State> getallStates()
	{
		try {
			List<State> allstates = (List<State>) executeHQL("from State");
			return allstates;
		} catch (Exception e) {
			throw new UnexpectedDBException("No state available in database", e);
		}
		
	} 
	
	

}
